home *** CD-ROM | disk | FTP | other *** search
-
- /*© Copyright 1988-1992 UserLand Software, Inc. All Rights Reserved.*/
-
-
- #include "appletmemory.h"
- #include "appletfont.h"
- #include "appletstrings.h"
- #include "appletquickdraw.h"
- #include "applettextedit.h"
-
-
-
- typedef struct tyscrolldata {
-
- Rect origdestrect;
-
- short origlen;
- } tyscrolldata;
-
- tyscrolldata scrolldata;
-
-
-
-
- static boolean getmacbuffer (hdleditrecord hbuffer, TEHandle *macbuffer) {
-
- if (hbuffer == nil)
- return (false);
-
- *macbuffer = (**hbuffer).macbuffer;
-
- return (*macbuffer != nil);
- } /*getmacbuffer*/
-
-
- static void editprescroll (hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- scrolldata.origdestrect = (**macbuffer).destRect;
-
- scrolldata.origlen = (**macbuffer).teLength;
- } /*editprescroll*/
-
-
- static void editsetscrollvalues (hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
- Rect destrect, viewrect;
- boolean flscrolled;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- flscrolled = scrolldata.origlen != (**macbuffer).teLength;
-
- if (!flscrolled)
- flscrolled = !equalrects (scrolldata.origdestrect, (**macbuffer).destRect);
-
- (**hbuffer).flscrolled = flscrolled;
-
- destrect = (**macbuffer).destRect;
-
- viewrect = (**macbuffer).viewRect;
-
- (**hbuffer).vertcurrent = viewrect.top - destrect.top;
-
- (**hbuffer).horizcurrent = viewrect.left - destrect.left;
- } /*editsetscrollvalues*/
-
-
- static short getTEjustification (tyjustification justification) {
-
- switch (justification) {
-
- case leftjustified:
- return (teJustLeft);
-
- case centerjustified:
- return (teJustCenter);
-
- case rightjustified:
- return (teJustRight);
- } /*switch*/
-
- return (teJustLeft);
- } /*getTEjustification*/
-
-
- boolean editnewbuffer (Rect r, boolean flwordwrap, bigstring bs, hdleditrecord *hrecord) {
-
- register hdleditrecord h;
- register TEHandle macbuffer;
-
- if (!newclearhandle (longsizeof (tyeditrecord), (Handle *) hrecord))
- return (false);
-
- h = *hrecord; /*copy into register*/
-
- InsetRect (&r, texthorizinset, textvertinset);
-
- macbuffer = TENew (&r, &r);
-
- if (macbuffer == nil) { /*memory error*/
-
- disposehandle ((Handle) h);
-
- *hrecord = nil;
-
- return (false);
- }
-
- if (flwordwrap)
- (**macbuffer).crOnly = 1;
- else
- (**macbuffer).crOnly = -1;
-
- TESetText (&bs [1], (long) stringlength (bs), macbuffer);
-
- TEActivate (macbuffer);
-
- (**h).macbuffer = macbuffer;
-
- return (true);
- } /*editnewbuffer*/
-
-
- void editupdate (hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
- Rect r;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- r = (**macbuffer).viewRect;
-
- TEUpdate (&r, macbuffer);
- } /*editupdate*/
-
-
- void editupdateport (hdleditrecord hbuffer, Rect rport, WindowPtr w) {
-
- /*
- 8/26/92 DW: update the TE handle, but do it in a port different from the
- one it lives inside of. need this to support printing in MinApp.
-
- 9/3/92 DW: add "rport" parameter, to allow the caller to determine the
- temporary viewRect for the TE buffer.
- */
-
- TEHandle macbuffer;
- Rect oldview, olddest;
- WindowPtr oldport;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- oldview = (**macbuffer).viewRect;
-
- olddest = (**macbuffer).destRect;
-
- oldport = (**macbuffer).inPort;
-
- (**macbuffer).viewRect = rport;
-
- (**macbuffer).destRect = rport;
-
- (**macbuffer).inPort = w;
-
- TEUpdate (&rport, macbuffer);
-
- (**macbuffer).viewRect = oldview;
-
- (**macbuffer).destRect = olddest;
-
- (**macbuffer).inPort = oldport;
- } /*editupdateport*/
-
-
- void editactivate (hdleditrecord hbuffer, boolean flactive) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- if (flactive)
- TEActivate (macbuffer);
- else
- TEDeactivate (macbuffer);
- } /*editupdate*/
-
-
- void editsetrect (hdleditrecord hbuffer, Rect r) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- InsetRect (&r, texthorizinset, textvertinset);
-
- (**macbuffer).viewRect = r;
-
- if (!(**hbuffer).flwindowbased)
- (**macbuffer).destRect = r;
-
- TECalText (macbuffer);
-
- /*editupdate (hbuffer);*/
- } /*editsetrect*/
-
-
- void editsetfont (hdleditrecord hbuffer, short fontnum, short fontsize) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- editprescroll (hbuffer);
-
- (**macbuffer).txFont = fontnum;
-
- (**macbuffer).txSize = fontsize;
-
- pushstyle (fontnum, fontsize, (**macbuffer).txFace);
-
- (**macbuffer).fontAscent = globalfontinfo.ascent;
-
- (**macbuffer).lineHeight = globalfontinfo.ascent + globalfontinfo.descent + globalfontinfo.leading;
-
- popstyle ();
-
- TECalText (macbuffer);
-
- editsetscrollvalues (hbuffer);
-
- (**hbuffer).flscrolled = true;
- } /*editsetfont*/
-
-
- void editautoscroll (hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- TEAutoView (true, macbuffer);
- } /*editautoscroll*/
-
-
- void editsetwordwrap (hdleditrecord hbuffer, boolean fl) {
-
- TEHandle macbuffer;
- register short cronly;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- if (fl)
- cronly = 1; /*word-wrap on*/
- else
- cronly = -1; /*no word-wrap*/
-
- (**macbuffer).crOnly = cronly;
- } /*editsetwordwrap*/
-
-
- void editdispose (hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- TEDeactivate (macbuffer); /*remove the caret cursor, or unhighlight selection*/
-
- TEDispose (macbuffer);
-
- disposehandle ((Handle) hbuffer);
- } /*editdispose*/
-
-
- void editdrawtext (bigstring bs, Rect r, tyjustification justification) {
-
- InsetRect (&r, texthorizinset, textvertinset);
-
- TextBox (&bs [1], stringlength (bs), &r, getTEjustification (justification));
- } /*editdrawtext*/
-
-
- void editdrawtexthandle (Handle htext, Rect r, tyjustification justification) {
-
- if (htext == nil)
- return;
-
- InsetRect (&r, texthorizinset, textvertinset);
-
- TextBox (*htext, GetHandleSize (htext), &r, getTEjustification (justification));
- } /*editdrawtexthandle*/
-
-
- void editidle (hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- TEIdle (macbuffer);
- } /*editidle*/
-
-
- void editcut (hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- editprescroll (hbuffer);
-
- TECut (macbuffer);
-
- editsetscrollvalues (hbuffer);
- } /*editcut*/
-
-
- void editcopy (hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- TECopy (macbuffer);
- } /*editcopy*/
-
-
- void editpaste (hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- editprescroll (hbuffer);
-
- TEPaste (macbuffer);
-
- editsetscrollvalues (hbuffer);
- } /*editpaste*/
-
-
- boolean editgettext (hbuffer, bs) hdleditrecord hbuffer; bigstring bs; {
-
- TEHandle macbuffer;
- register int len;
-
- setstringlength (bs, 0);
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return (false);
-
- len = (**macbuffer).teLength;
-
- setstringlength (bs, len);
-
- moveleft (*(**macbuffer).hText, &bs [1], (long) len);
-
- return (true);
- } /*editgettext*/
-
-
- void editkeystroke (char chkb, hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- editprescroll (hbuffer);
-
- TEKey (chkb, macbuffer);
-
- editsetscrollvalues (hbuffer);
- } /*editkeystroke*/
-
-
- void editclick (Point pt, boolean flshiftkey, hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- editprescroll (hbuffer);
-
- pushclip ((**macbuffer).viewRect);
-
- TEClick (pt, flshiftkey, macbuffer);
-
- popclip ();
-
- editsetscrollvalues (hbuffer);
- } /*editclick*/
-
-
- boolean editsetselection (hdleditrecord hbuffer, short selstart, short selend) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return (false);
-
- editprescroll (hbuffer);
-
- TESetSelect (selstart, selend, macbuffer);
-
- editsetscrollvalues (hbuffer);
-
- return (true);
- } /*editgetselection*/
-
-
- void editselectall (hdleditrecord hbuffer) {
-
- editsetselection (hbuffer, 0, 32767);
- } /*editselectall*/
-
-
- boolean editgetselection (hdleditrecord hbuffer, short *selstart, short *selend) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return (false);
-
- *selstart = (**macbuffer).selStart;
-
- *selend = (**macbuffer).selEnd;
-
- return (true);
- } /*editgetselection*/
-
-
- boolean editpointinrect (Point pt, hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return (false);
-
- return (PtInRect (pt, &(**macbuffer).viewRect));
- } /*editpointinrect*/
-
-
- boolean editreplace (bigstring bs, hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return (false);
-
- editprescroll (hbuffer);
-
- if ((**macbuffer).selStart != (**macbuffer).selEnd)
- TEDelete (macbuffer);
-
- TEInsert (&bs [1], stringlength (bs), macbuffer);
-
- editsetscrollvalues (hbuffer);
-
- return (true); /*TE gives no way to return an error*/
- } /*editreplace*/
-
-
- void editsetjustification (tyjustification justification, hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- TESetJust (getTEjustification (justification), macbuffer);
- } /*editsetjustification*/
-
-
- void editgetsize (Handle htext, short *height, short *width) {
-
- /*
- return the size of a rectangle that could contain the indicated string
- observing the defaults for inset both vertically and horizontally, and
- a minimum width for text display.
-
- assumes that the font/size/style info are correct in globalfontinfo.
-
- 8/8/92 DW: upgrade to not use a bigstring to hold the text to be measured.
- we were encountering lines that were longer than 255 chars.
- */
-
- long textlength;
- long i;
- short ctlines = 1;
- short maxwidth = 0;
- short vertpixels;
- short ixlinestart = 0, linelength = 0;
-
- textlength = GetHandleSize (htext);
-
- for (i = 0; i <= textlength; i++) {
-
- char ch = (*htext) [i];
-
- if ((ch == chreturn) || (i == textlength)) {
-
- short w;
-
- w = TextWidth (*htext, ixlinestart, linelength);
-
- if (w > maxwidth)
- maxwidth = w;
-
- if (ch == chreturn)
- ctlines++;
-
- ixlinestart += linelength + 1;
-
- linelength = 0;
- }
- else
- linelength++;
- } /*for*/
-
- maxwidth = max (maxwidth, mintextwidth);
-
- maxwidth += 2 * texthorizinset;
-
- maxwidth++; /*ask for one extra pixel, work around TE display anomaly*/
-
- *width = maxwidth;
-
- vertpixels = ctlines * (globalfontinfo.ascent + globalfontinfo.descent + globalfontinfo.leading);
-
- *height = vertpixels + (2 * textvertinset);
- } /*editgetsize*/
-
-
- void editgetbuffersize (hdleditrecord hbuffer, short *height, short *width) {
-
- /*
- 8/8/92 DW for Snuffy: can't rely on the value for height returned by editgetsize,
- since it doesn't take word-wrapping into account.
- */
-
- TEHandle macbuffer;
- char lastchar;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- editgetsize ((**macbuffer).hText, height, width);
-
- if ((**hbuffer).flwindowbased) {
-
- *height = ((**macbuffer).nLines * (**macbuffer).lineHeight) + globalfontinfo.descent;
-
- lastchar = (*(**macbuffer).hText) [(**macbuffer).teLength - 1];
-
- if (lastchar != chreturn) /*a quirk of TE*/
- *height += (**macbuffer).lineHeight;
-
- if ((**macbuffer).crOnly == 1) { /*word-wrapping turned on*/
-
- Rect r = (**macbuffer).destRect;
-
- *width = r.right - r.left;
- }
- }
-
- *width += 2 * texthorizinset;
-
- *height += 2 * textvertinset;
- } /*editgetbuffersize*/
-
-
- boolean editgettexthandle (hdleditrecord hbuffer, Handle *htext) {
-
- /*
- return the text handle from the edit buffer. do not dispose the handle or
- alter the length of the text.
- */
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return (false);
-
- *htext = (**macbuffer).hText;
-
- return (true);
- } /*editgettexthandle*/
-
-
- boolean editnewbufferfromhandle (Rect r, boolean flwordwrap, Handle htext, hdleditrecord *hrecord) {
-
- register hdleditrecord h;
- register TEHandle macbuffer;
-
- if (!newclearhandle (longsizeof (tyeditrecord), (Handle *) hrecord))
- return (false);
-
- h = *hrecord; /*copy into register*/
-
- InsetRect (&r, texthorizinset, textvertinset);
-
- macbuffer = TENew (&r, &r);
-
- if (macbuffer == nil) { /*memory error*/
-
- disposehandle ((Handle) h);
-
- *hrecord = nil;
-
- return (false);
- }
-
- if (flwordwrap)
- (**macbuffer).crOnly = 1;
- else
- (**macbuffer).crOnly = -1;
-
- lockhandle (htext);
-
- TESetText (*htext, GetHandleSize (htext), macbuffer);
-
- unlockhandle (htext);
-
- TEActivate (macbuffer);
-
- (**h).macbuffer = macbuffer;
-
- return (true);
- } /*editnewbufferfromhandle*/
-
-
- boolean editsettexthandle (hdleditrecord hrecord, Handle htext) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hrecord, &macbuffer))
- return (false);
-
- lockhandle (htext);
-
- TESetText (*htext, GetHandleSize (htext), macbuffer);
-
- unlockhandle (htext);
-
- return (true);
- } /*editsettexthandle*/
-
-
- boolean editgettexthandlecopy (hdleditrecord hbuffer, Handle *htext) {
-
- /*
- caller must dispose of the handle we allocate.
-
- 7/8/92 DW: if there is no text, return true with htext == nil.
- */
-
- TEHandle macbuffer;
- Handle h;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return (false);
-
- h = (**macbuffer).hText;
-
- if (GetHandleSize (h) == 0) {
-
- *htext = nil;
-
- return (true);
- }
-
- return (copyhandle (h, htext));
- } /*editgettexthandlecopy*/
-
-
- boolean edithaveselection (hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return (false);
-
- return ((**macbuffer).selStart != (**macbuffer).selEnd);
- } /*edithaveselection*/
-
-
- boolean editscroll (hdleditrecord hbuffer, short dh, short dv) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return (false);
-
- editprescroll (hbuffer);
-
- TEScroll (dh, dv, macbuffer);
-
- editsetscrollvalues (hbuffer);
-
- return (true);
- } /*edithaveselection*/
-
-
- void editscrollto (hdleditrecord hbuffer, short h, short v) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return;
-
- editsetscrollvalues (hbuffer);
-
- editscroll (hbuffer, (**hbuffer).horizcurrent - h, (**hbuffer).vertcurrent - v);
- } /*editscrollto*/
-
-
- boolean editreplacehandle (Handle htext, hdleditrecord hbuffer) {
-
- TEHandle macbuffer;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return (false);
-
- editprescroll (hbuffer);
-
- if ((**macbuffer).selStart != (**macbuffer).selEnd)
- TEDelete (macbuffer);
-
- lockhandle (htext);
-
- TEInsert (*htext, GetHandleSize (htext), macbuffer);
-
- unlockhandle (htext);
-
- editsetscrollvalues (hbuffer);
-
- return (true); /*TE gives no way to return an error*/
- } /*editreplacehandle*/
-
-
- boolean editgetselectedtexthandle (hdleditrecord hbuffer, Handle *htext) {
-
- TEHandle macbuffer;
- short selstart, selend;
- short lensel;
- Handle h;
- boolean fl;
-
- if (!getmacbuffer (hbuffer, &macbuffer))
- return (false);
-
- selstart = (**macbuffer).selStart;
-
- selend = (**macbuffer).selEnd;
-
- lensel = selend - selstart;
-
- if (lensel < 0)
- lensel = 0;
-
- h = (**macbuffer).hText;
-
- lockhandle (h);
-
- fl = newfilledhandle (&((*h) [selstart]), (long) lensel, htext);
-
- unlockhandle (h);
-
- return (fl);
- } /*editgetselectedtexthandle*/
-
-
-
-
-
-